package com.guohu.controller;

import com.bean.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guohu.service.GuoHuService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RequestMapping("guohu")
@Controller
public class GuoHuController {
    @Autowired
    GuoHuService guoHuService;

    //跳转guohu
    @RequestMapping("guohu.do")
    public String guohu(HttpServletRequest request){
        //获取过户单号 uuid生成
        String Str1= UUID.randomUUID().toString().replace("-", "");
        String guohuID = Str1.substring(0,10);
        request.getSession().setAttribute("guohuID",guohuID);
        return "guohu/guohu";
    }
    //跳转guohuManage
    @RequestMapping("guohuManage.do")
    public String guohudan(){
        return "guohu/guohuManage";
    }

    //卖家
    @RequestMapping("getSeller.do")
    @ResponseBody
    public List<WmsTCustomer> selSeller(){
        List<WmsTCustomer> guohuSeller = guoHuService.selSeller();
        System.out.println(guohuSeller);
        return guohuSeller;
    }
    //买家
    @RequestMapping("getBuyer.do")
    @ResponseBody
    public List<WmsTCustomer> selBuyer(){
        List<WmsTCustomer> guohuBuyer = guoHuService.selBuyer();
        System.out.println(guohuBuyer);
        return guohuBuyer;
    }
    //显示卖家下的所有货物
    @RequestMapping("getDetails.do")
    public String selDetails(String maijia,String maijia1,String date,HttpServletRequest request){
        /*System.out.println("maijia"+maijia1+"qe);*/
        String[] d =  maijia1.split("-");
        //买家id
        String mj1= d[0];
        //买家姓名
        String mj = d[1];
        System.out.println("买家姓名"+mj);
        String[] f = maijia.split("-");
        //卖家id
        String a = f[0];
        //卖家姓名
        String b = f[1];
        System.out.println("这是a:"+a);
        //过户时间
        request.getSession().setAttribute("guohushijian",date);
        //买家id
        request.getSession().setAttribute("mjID",mj1);
        //买家姓名
        request.getSession().setAttribute("mjname",mj);
        //卖家id
        request.getSession().setAttribute("maijisID",a);
        //卖家姓名
        request.getSession().setAttribute("maijisname",b);
        List<WmsTBillDetails> wmsTBillDetails1 = guoHuService.selDetails(a);
        System.out.println(wmsTBillDetails1);
       request.getSession().setAttribute("guohuxuanhuo",wmsTBillDetails1);
        System.out.println(wmsTBillDetails1);
        return "guohu/guohuxuanhuo";
    }
    //过户单
    @RequestMapping("getGuohuDan.do")
    public String selGuoHuDan(String detailsid,String detailsNum,HttpServletRequest request){
        System.out.println("货物id"+detailsid);
        System.out.println("物品数量"+detailsNum);
        request.getSession().setAttribute("huowuid",detailsid);
        request.getSession().setAttribute("huowushuliang",detailsNum);
        WmsTBillDetails wmsTBillDetails = guoHuService.selDetailID(detailsid);
        System.out.println(wmsTBillDetails);
        request.getSession().setAttribute("guohudan",wmsTBillDetails);
        return "guohu/guohudan";
    }
    //过户数据
    @RequestMapping("addTrans.do")
    public String addOrders(String qita,String yingshou,String fufei,String kucun,HttpServletRequest request){
        System.out.println("其他费用"+qita+"库存费用"+kucun+"合计费用"+yingshou+"实收费用"+fufei);

        //插入表wms_t_transfer
        //生成过户单系统编号
        String Str1= UUID.randomUUID().toString().replace("-", "");
        String transferID = Str1.substring(0,10);
        //管理员姓名
        WmsTUser wmsTUser = (WmsTUser) request.getSession().getAttribute("load_user");
        String userID = wmsTUser.getUserID();
        //卖方id
        String transferVender = (String) request.getSession().getAttribute("maijisname");
        //买方id
        String transferBuyer = (String) request.getSession().getAttribute("mjname");
        //日期
        String transferDate = (String) request.getSession().getAttribute("guohushijian");
        //交易单号
        String transferCode = (String) request.getSession().getAttribute("guohuID");
        System.out.println("生成过户单系统号"+transferID+"管理员姓名"+userID+"卖方"+transferVender+"买方"+
                transferBuyer+"日期"+transferDate+"交易单号"+transferCode);
        //插入wms_t_transfer
        guoHuService.inTransfer(transferID,userID,transferVender,transferBuyer,transferDate,transferCode);

        //数据插入wms_t_trans_details
        //生成过户单明细编号
        String Str2= UUID.randomUUID().toString().replace("-", "");
        String tdetailsID= Str2.substring(0,10);
        //获取业务单明细编号
        String detailsID = (String) request.getSession().getAttribute("huowuid");
        //获取商品数量
        String tdetailsGoodsNum = (String) request.getSession().getAttribute("huowushuliang");
        System.out.println("生成过户单明细编号"+tdetailsID+"生成过户单系统编号"+transferID+"获取业务单明细编号"+detailsID+"获取商品数量"+tdetailsGoodsNum);
        guoHuService.inTranDeta(tdetailsID,transferID,detailsID,tdetailsGoodsNum,transferDate);

        //卖家插入bill表
        //生成billID
        String Str3= UUID.randomUUID().toString().replace("-", "");
        String billID = Str3.substring(0,10);
        //卖家的id
        String maijiaID = (String) request.getSession().getAttribute("maijisID");
        //调用service插入表
        guoHuService.inbillMaij(billID,userID,maijiaID,transferDate,transferCode);

        //买家插入bill表
        String Str4= UUID.randomUUID().toString().replace("-", "");
        String biID = Str4.substring(0,10);
        //获取买家id
        String mj = (String) request.getSession().getAttribute("mjID");
        System.out.println("买家id"+mj);
        guoHuService.inbillmj(biID,userID,mj,transferDate,transferCode,kucun,qita,yingshou,fufei);
        return "guohu/guohu";
    }
    //动态查询
    @RequestMapping("selectTransfer.do")
    public String selTransFer(String transferBuyer, String transferVender,String date1, String date2,String pageSize, String pageNum,HttpServletRequest request){
        System.out.println("买家"+transferBuyer+"卖家"+transferVender+"日期1:"+date1+"日期2:"+date2);

        //数据放域 回显页面
        request.getSession().setAttribute("guohmaij",transferBuyer);
        request.getSession().setAttribute("guohmaija",transferVender);
        request.getSession().setAttribute("guohudate1", date1);
        request.getSession().setAttribute("guohudate2", date2);

        //分页
        if (pageSize == null || pageSize.equals("")) {
            PageHelper.startPage(1, 5);
            //pageSize & pageNum 放域
            request.getSession().setAttribute("num156", 1);
            request.getSession().setAttribute("size156", 5);
        } else {
            PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
            //pageSize & pageNum 放域
            request.getSession().setAttribute("num156", Integer.parseInt(pageNum));
            request.getSession().setAttribute("size156", Integer.parseInt(pageSize));
        }

        //查询数据
        List<WmsTTransfer> wmsTTransferss = guoHuService.selTransFer(transferBuyer,transferVender,date1,date2);
        System.out.println(wmsTTransferss);

        PageInfo<List<WmsTTransfer>> pageInfo = new PageInfo(wmsTTransferss);
        request.getSession().setAttribute("guohupageInfo",pageInfo);
        System.out.println("pageInfo:");
        System.out.println(pageInfo);
        return "guohu/guohuManage";
    }
}